#!/bin/bash
fromelf="C:/Keil_v5/ARM/ARMCC/bin/fromelf.exe"		#locates fromelf.exe, usually in C:/Keil/ARM/ARMCC/bin/, C:/Keil/ARM/BIN40 or similar...
axfPath="../ObjFlash"							#locates directory which contains the generated .axf file
outputPath="../_firmware_"
xml=$outputPath/conf.xml						#specifies the target xml config file
CRC32="../Tools/CRC32.exe"
buildlog=$outputPath/build.log
binPcsPath=$outputPath/binPcs
binSpliter="../Tools/BinSpliter.exe"
rlscfg="../APP/ReleaseCfg.h"
f1_loader=$outputPath/f1_loader.hex
f1_ucosii=$outputPath/f1_ucosii.hex
#f1_bootable=$outputPath/f1_bootable.hex
python="C:/Python27/python.exe"
glue=" ../Tools/glue.py"

echo "Hello World!This GKoson DIY"
echo "Today is" `date +"%Y.%m.%d"` .
date=$(echo `date +"%Y.%m.%d"`)
echo $date

revision=$(grep -E 'SVN_REVISION' $rlscfg | awk '/[0-9]+/{print $3}')	    #retrieve SVN_REVISION in file ReleaseCfg.h
echo $revision

## James commented, DONT parse 'FIRMWARE_VERSION' to format VxxRxx string again!
macros=`echo 1 | awk -F "[()]" '/^\#.*FIRMWARE_VERSION/{
	do
	{
		for(i=2;i<=NF;i+=2)
		{
			print $i;
		}
		getline;
	}
	while($1~'/^[[:space:]]*\(\#\#\|[a-zA-Z]\)/');
}' $rlscfg`

for var in ${macros[@]}
do
	devtyp=$(echo $devtyp`grep "\#define[[:space:]]\+$var" $rlscfg | awk '/[0-9]+/{print $3}'`)
done

#version=$(echo "V"$devtyp"R"$revision)
version=10010057V1.0.1
f1_bootable=$outputPath/f1_bootable_THIS_IS_NEW_$version.hex

if [ ! -n $version ]
then
	echo "Firmware Version not detected, make sure the right input file: $rlscfg is imported properly or key word \"SVN_REVISION\" is define in the file."
	exit 2
fi

if [ ! -d $outputPath ]
then
	echo "$outputPath does not exist, Creating dir $outputPath ..."
	mkdir $outputPath
fi

if [ -e $outputPath/*.bin ] 
then
	echo "Removing old binary files"
	rm $outputPath/*.bin $binPcsPath/*.bin
fi

if [ -e $outputPath/$f1_bootable ] && [ -e $outputPath/$f1_ucosii ]
then
	echo "Removing old hex files"
	rm $outputPath/$f1_bootable $outputPath/$f1_ucosii
fi
	
echo "Converting .axf file to raw binary..."
if [ ! -e $axfPath/*.axf ]
then
	echo "Fatal Error, .axf file not found, please specify one..."
	exit
elif [ ! -e $fromelf ]
then
	echo "Convert Tools not properly specified, Please check the path of fromelf.exe under Keil/ARM/..."
	exit
else
	$fromelf --bin -o $outputPath/f1_ucosii.bin $axfPath/*.axf
	$fromelf --i32 -o $outputPath/$f1_ucosii $axfPath/*.axf #create intel format hex
fi

if [ ! -e $outputPath/*.bin ]
then
	echo "Raw binary file failed to create, Abort!"
	exit
fi

if [ ! -e $outputPath/$f1_loader ] || [ ! -e $outputPath/$f1_ucosii ]
then
	echo "$f1_loader or $f1_ucosii is missing, Abort!"
	exit
fi

if [ ! -e $glue ]
then
	echo "Python script $glue is not found, Abort!"
else
	$python $glue $f1_loader $f1_ucosii $f1_bootable
fi	

echo "Calculating CRC32 checksum of binary file, result saved in $buildlog"
if [ ! -e $CRC32 ]
then
	echo "Tool $CRC32 does not exist, please specify the tool path..."
	exit
else
	if [ ! -e $buildlog ]
	then
		touch $buildlog #create file $buildlog
	fi
	$CRC32 $outputPath/*.bin > $buildlog
fi

fwsize=$(grep -E 'FILE_SIZE:' $buildlog | grep -o -E '[0-9]+')
fwchksum=$(grep -E 'CHECK_SUM:' $buildlog | grep -o -E '[A-F0-9]{8}')
#echo "firmware size $fwsize, check sum $fwchksum"

echo "Splitting binary file into pieces, each sized at 1kB..."
if [ ! -d $binPcsPath ]
then
	mkdir $binPcsPath
fi

if [ ! -e $binSpliter ]
then
	echo "Tool $binSpliter does not exist, please specify the tool path..."
	exit
else
	$binSpliter $outputPath/*.bin $binPcsPath/ >> $buildlog
fi

modauther=$(echo `whoami` | awk -F "\\" '{print $2}')
if [ ! -n $modauther ]
then
	modauther=$(grep "<mauth>" $xml | awk -F "[><]" '{print $2}')
fi	
# echo $modauther

sed -i "s/\(<mauth>\).*\(<\/mauth>\)/\1$modauther\2/;
	s/\(<date>\).*\(<\/date>\)/\1$date\2/;
	s/\(<ver>\).*\(<\/ver>\)/\1$version\2/;
	s/\(<len>\).*\(<\/len>\)/\1$fwsize\2/;
	s/\(<sum>\).*\(<\/sum>\)/\1$fwchksum\2/" $xml

#echo "Device Type: $devtyp"
#echo "Firmware Revision: $version, Firmware Size: $fwsize, Checksum: 0x$fwchksum."
echo -e "\nProcessing Result..."
cat $xml
echo "All Done!!! Exit..."
exit


